const getQueryString = function (name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
};

window.getQueryString = getQueryString;
window.from = getQueryString("from");
window.token = getQueryString("token");

const gankeLogo =
  "http://leiting-shequ-image.oss-cn-shanghai.aliyuncs.com/leiting/8767108a330611eb8ce49d4af5205ea1.png";

// 安卓ios函数调用
window.injectedAll = {
  openLogin: () => {
    window.gankeTool.openLogin();
    // if (getQueryString("from") === "android") {
    //   window.injectedObject.openLogin();
    // } else {
    //   window.webkit.messageHandlers.openLogin.postMessage({});
    // }
  },
  shareInit(type) {
    let url = window.location.origin + window.location.pathname;
    let title = "",
      desc = "",
      link = url,
      imgUrl = "";
    let data = window.shareData;
    if (type == "list") {
      title = "三国志战略版武将战法图鉴大全 -  Ganke游戏社区";
      desc = "你再也找不到比这更全更好用的三战武将图鉴了！";
      imgUrl = gankeLogo;
    } else if (type == "heroe") {
      title = data.name + " - 武将介绍及战法搭配推荐";
      desc = data.intro;
      imgUrl = data.avatar;
    } else if (type === "tactic") {
      title = data.name + " - 战法介绍及搭配推荐";
      desc = data.info.substr(0, 100);
      imgUrl = gankeLogo;
    } else if (type === "match") {
      title = "配将助手 - 三国志战略版热门阵容搭配推荐 -  Ganke游戏社区";
      desc = "三战热门强力阵容推荐，根据拥有武将自动匹配可用阵容。";
      imgUrl = gankeLogo;
    } else if (type === "forms") {
      title = data.title + " -  Ganke游戏社区";
      desc = data.description;
      imgUrl = data.imgurl;
    }

    let share = {
      title: title || "三国志战略版武将战法图鉴大全 -  Ganke游戏社区",
      desc: desc || "你再也找不到比这更全更好用的三战武将图鉴了！",
      link: link,
      imgUrl: imgUrl || gankeLogo,
    };
    window.gankeTool.shareInit(share);
  },
  share: () => {
    window.gankeTool.share();
  },
  download() {
    /// 判断当前页面是图鉴还是搭配
    let path = window.location.pathname.substr(6);
    let match = ["/match", "/myheroe", "/enlist", "/publish", "/forms"];
    let isMatch = false;
    match.some((item) => {
      if (path.indexOf(item) === 0) isMatch = true;
      return isMatch;
    });

    try {
      // window.gankeTool.download();
      window.gankeTool.download({
        tool_name: isMatch ? "三国志.战略版搭配" : "三国志.战略版图鉴",
        channel_name: "三国志.战略版",
        channel_id: "1000055",
      });
    } catch (e) {
      console.error("下载统计异常", e);
    }
  },
  finishWebView: () => {
    window.gankeTool.finishWebView();
  },
  /**
   * 发送UV事件
   * @param {*} tool_name
   */
  sendUv(tool_name) {
    // 发送页面UV统计
    try {
      window.gankeTool.collectEvent("web_view_page", {
        ditch: true,
        module_name: "tool",
        // tool_name: '三国志.战略版图鉴',
        // tool_name: "三国志.战略版搭配",
        tool_name: tool_name,
        channel_name: "三国志.战略版",
        channel_id: "1000055",
      });
    } catch (e) {
      console.error("页面UV统计埋点异常", e);
    }
  },
  openPost: (id) => {
    if (getQueryString("from") === "android") {
      window.injectedObject.openPost(id);
    } else {
      window.webkit.messageHandlers.openLogin.openPost({
        postid: id,
      });
    }
  },
  openInfoPieces: (id) => {
    if (getQueryString("from") === "android") {
      window.injectedObject.openInfoPieces(id);
    } else {
      window.webkit.messageHandlers.openInfoPieces.postMessage({
        chipid: id,
      });
    }
  },
  openUserDetail: (id) => {
    if (getQueryString("from") === "android") {
      window.injectedObject.openUserDetail(id);
    } else {
      window.webkit.messageHandlers.openUserDetail.postMessage({
        userid: id,
      });
    }
  },
};

// 检查未登录
if ((window.from === "android" || window.from === "ios") && !window.token) {
  window.injectedAll.openLogin(); // 跳转登录
  throw Error("未登录"); // 抛出异常
}
